package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.dto.XeVenPurchDTO;
import com.xebest.api.dto.XeVenPurchPageDTO;
import com.xebest.api.dto.XeVenPurchResponseDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.config.ParamConfig;
import com.xebest.constant.XeConstans;
import com.xebest.constant.XeErrorCode;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenPurchForm;
import com.xebest.web.form.XeVenStoreInfoForm;
import com.xebest.web.service.XeVenPurchService;
import com.xebest.web.service.XeVenStoreInfoService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenPurchView;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 鲜易云管理平台采购单管理
 *
 * @author 袁世林
 */

@RestController
@RequestMapping("restApi/purchManage")
public class XeVenPurchManageRestApi extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeVenPurchManageRestApi.class);

    @Autowired
    private ParamConfig paramConfig;//获取公共配置信息
    private String picDir = XeConstans.ERP_PRO_PIC_DIR;//图片中缀

    @Autowired
    private XeVenStoreInfoService xeVenStoreInfoService;

    @Autowired
    private XeVenPurchService xeVenPurchService;

    @Autowired
    private XeBaseUtil xeBaseUtil;


    /**
     * 获取仓库名称列表信息
     *
     * @author 袁世林
     */
    @RequestMapping("queryStoreNameList")
    public BaseResponse queryStoreNameList(@RequestBody XeVenStoreInfoForm form) {

        log.info("获取仓库名称列表信息，请求参数为{}", JSON.toJSONString(form));
        //设置查询参数
        Map<String, Object> params = new HashMap<String, Object>();
        //添加供应商编码
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        params.put("userCode", userInfo.getUserCode());
        params.put("form", form);
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            List<Map<String, Object>> storeInfoList = this.xeVenStoreInfoService.listStoreName(params);
            map.put("actionList", storeInfoList);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("--------------获取仓库名称列表信息 异常为{}", e);
            return sendAjaxError();
        }
    }


    /**
     * 鲜易云管理平台采购单管理-----查询采购单列表
     *
     * @author 袁世林
     */
    @RequestMapping("queryPurchList")
    @ResponseBody
    public XeVenPurchPageDTO queryPurchList(@RequestBody XeVenPurchDTO XeVenPurchDTO) {
        log.info("--------------查询采购单列表 start-------------------");
        // 1、验证参数
        // 2、拼装参数
        if (null == XeVenPurchDTO) {
            XeVenPurchDTO = new XeVenPurchDTO();
        }

        XeVenPurchPageDTO xeVenPurchPageDTO = new XeVenPurchPageDTO();

        PageHelper.startPage(XeVenPurchDTO.getXePageDto().getPageNum(), XeVenPurchDTO.getXePageDto().getPageSize());
        String formJson = JSONObject.toJSONString(XeVenPurchDTO);
        // 查询条件封装
        XeVenPurchForm form = JSONObject.parseObject(formJson, XeVenPurchForm.class);

        try {
            // 3、输出结果
            List<XeVenPurchView> purchList = xeVenPurchService.queryPurchListRestApi(form);

            List<XeVenPurchDTO> XeVenPurchDTOList = JSONArray.parseArray(JSONArray.toJSONString(purchList),
                    XeVenPurchDTO.class);
            XePageInfoDto pageInfo = new XePageInfoDto<>(purchList);
            xeVenPurchPageDTO.setList(XeVenPurchDTOList);
            xeVenPurchPageDTO.setXePageInfoDto(pageInfo);
            xeVenPurchPageDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);

            return xeVenPurchPageDTO;
        } catch (Exception e) {
            log.error("查询采购单列表queryPurchList()异常：{}", e);
            xeVenPurchPageDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        } finally {
            log.info("--------------查询采购单列表end-------------------");
        }
        return xeVenPurchPageDTO;
    }

    /**
     * 鲜易云管理平台采购单管理-----查询采购单详情
     *
     * @author 袁世林
     */
    @RequestMapping("queryPurchDetail")
    @ResponseBody
    public XeVenPurchResponseDTO queryPurchDetail(@RequestBody XeVenPurchDTO XeVenPurchDTO) {
        log.info("--------------查询采购详情start-------------------");
        XeVenPurchResponseDTO responseDTO = new XeVenPurchResponseDTO();
        // 验证参数
        boolean parError = true;
        if (null != XeVenPurchDTO
                && StringUtils.isNotBlank(XeVenPurchDTO.getPurchOrder())
                && null != XeVenPurchDTO.getVersion()) {
            parError = false;
        }
        if (parError) {
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
            responseDTO.setMsg("缺少必要参数！！！");
            return responseDTO;
        }
        try {
            String formJson = JSONObject.toJSONString(XeVenPurchDTO);
            // 查询条件封装
            XeVenPurchForm form = JSONObject.parseObject(formJson, XeVenPurchForm.class);

            XeVenPurchView purch = xeVenPurchService.queryPurchDetailRestApi(form);


            if (null != purch && StringUtils.isNotBlank(purch.getPurchOrder())) {

                String detailStr = JSONObject.toJSONString(purch);
                responseDTO = JSONObject.parseObject(detailStr, XeVenPurchResponseDTO.class);

                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
                String picServer = paramConfig.getImgUrl();//图片服务器地址
                responseDTO.setPicServer(picServer);
                responseDTO.setPicDir(picDir);
            }
            return responseDTO;
        } catch (Exception e) {
            log.error("查询采购详情queryPurchDetail()异常：{}", e);
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
            responseDTO.setMsg("查询采购详情queryPurchDetail()异常：{}");
            return responseDTO;
        }
    }


    /**
     * 导出采购单列表
     *
     * @author 袁世林
     */
    @RequestMapping(value = "exportPurchList")
    @ResponseBody
    public List<XeVenPurchView> exportPurchList(@RequestBody XeVenPurchDTO xeVenPurchDTO) {
        try {
            log.info("--------------查询采购单列表 start-------------------");
            // 1、验证参数
            // 2、拼装参数
            if (null == xeVenPurchDTO) {
                xeVenPurchDTO = new XeVenPurchDTO();
            }
            String formJson = JSONObject.toJSONString(xeVenPurchDTO);
            // 查询条件封装
            XeVenPurchForm form = JSONObject.parseObject(formJson, XeVenPurchForm.class);
            //form.setProNum("1"); //导出业务标记
            log.info("--------------form-------------------" + JSONObject.toJSONString(form));
            // 3、查询数据
            return xeVenPurchService.queryPurchListRestApi(form);
        } catch (Exception e) {
            log.error("导出采购单列表异常：{}", e);
        }
        return null;
    }

    /**
     * 导出采购单详情
     *
     * @author 袁世林
     */
    @RequestMapping("exportPurchDetail")
    @ResponseBody
    public List<XeVenPurchView> exportPurchDetail(@RequestBody XeVenPurchDTO xeVenPurchDTO) {
        try {
            log.info("--------------导出采购单详情 start-------------------");
            // 1、验证参数
            // 2、拼装参数
            if (null == xeVenPurchDTO) {
                xeVenPurchDTO = new XeVenPurchDTO();
            }
            String formJson = JSONObject.toJSONString(xeVenPurchDTO);
            // 查询条件封装
            XeVenPurchForm form = JSONObject.parseObject(formJson, XeVenPurchForm.class);
            // 3、查询数据
            return xeVenPurchService.exportPurchDetail(form);

        } catch (Exception e) {
            log.error("导出采购单列表异常：{}", e);
        }
        return null;

    }

}
